capture log close
log using gtiffcal_example.log, replace
* Example1 :Set up the Java dependence

//manually download the GeoTools package from Sourceforge https://sourceforge.net/projects/geotools/files/GeoTools\%2032\%20Releases/32.0/ and specify the path to "geotools-32.0/lib" 

geotools_init "C:/Users/17286/Documents/geotools-32.0/lib/"

*Example 2: Display the Metadata with gtiffinfo
gtiffdisp DMSP-like2020.tif

*Example 3: Read the nightlight data  for a specific region
shp2dta using "hunan.shp", database(hunan_db) coordinates(hunan_coord) genid(id)
use "hunan_coord.dta",clear
drop if missing(_X, _Y)
crsconvert _X _Y, gen(alber_) from(hunan.shp) to(DMSP-like2020.tif)

qui sum alber__X
local maxX = r(max)+2000
local minX = r(min)-2000

qui sum alber__Y
local maxY = r(max)+2000
local minY = r(min)-2000

gtiffread DMSP-like2020.tif, origin(1 1) size(-1 1) clear 
gen n=_n
sum n if y>`minY' & y<`maxY'
local start_row = r(min)
local n_rows = r(N)

gtiffread DMSP-like2020.tif, origin(1 1) size(1 -1) clear 
gen n=_n
sum n if x>`minX' & x<`maxX'
local start_col = r(min)
local n_cols = r(N)

gtiffread DMSP-like2020.tif, origin(`start_row' `start_col') size(`n_rows' `n_cols') clear

save DMSP-like2020.dta,replace

//heatplot
ssc install heatplot, replace
ssc install palettes, replace
ssc install colrspace, replace

use DMSP-like2020.dta, clear

heatplot value y x, color(plasma) ///
    keylabels(, format(%4.2f))

*Example 4：Calculating Average and Total Nighttime Light Intensity for Hunan
gzonalstats DMSP-like2020.tif using hunan.shp, stats("sum avg") clear
list z_Name avg sum
save "hunan_light.dta", replace

//presents TNLI and ANLI in Hunan

// The hunan.shp has been converted to a dta in example3.
//shp2dta using "hunan.shp", database(hunan_db) coordinates(hunan_coord) genid(id)

use "hunan_light.dta" ,clear
rename z_Name Name
merge 1:1 Name using hunan_db.dta,nogen

spmap sum using "hunan_coord.dta", id(id) clmethod(q) cln(6) fcolor(Heat) title("Total Night Light Index")
graph save graph1, replace

spmap avg using "hunan_coord.dta", id(id) clmethod(q) cln(6) fcolor(Heat) title("Average Night Light Index")  
graph save graph2, replace

graph combine graph1.gph graph2.gph

log close

